package com.kds.smarthome.service.impl;

import com.kds.smarthome.entity.QuartzJobLog;
import com.kds.smarthome.mapper.QuartzJobLogMapper;
import com.kds.smarthome.service.QuartzJobLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时任务日志服务实现类
 */
@Slf4j
@Service
@Transactional
public class QuartzJobLogServiceImpl implements QuartzJobLogService {

    @Autowired
    private QuartzJobLogMapper quartzJobLogMapper;

    @Override
    public void saveJobLog(QuartzJobLog jobLog) {
        try {
            quartzJobLogMapper.insert(jobLog);
        } catch (Exception e) {
            log.error("保存任务日志失败", e);
            throw new RuntimeException("保存任务日志失败", e);
        }
    }

    @Override
    public QuartzJobLog getJobLogById(Long logId) {
        return quartzJobLogMapper.selectById(logId);
    }

    @Override
    public List<QuartzJobLog> getJobLogs(String jobId) {
        return quartzJobLogMapper.selectByJobId(jobId);
    }

    @Override
    public List<QuartzJobLog> getRecentJobLogs(String jobId, Integer count) {
        if (count == null || count <= 0) {
            count = 10; // 默认查询最近10条
        }
        return quartzJobLogMapper.selectRecentByJobId(jobId, count);
    }

    @Override
    public List<QuartzJobLog> getJobLogsByCondition(String jobName, String jobGroup, Integer status, 
                                                   Date startTime, Date endTime) {
        return quartzJobLogMapper.selectByCondition(null, jobName, jobGroup, status, startTime, endTime, null, null);
    }
    
    @Override
    public Map<String, Object> getJobLogsByPage(String jobId, String jobName, String jobGroup, Integer status, 
                                              Date startTime, Date endTime, Integer pageNum, Integer pageSize) {
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        
        int offset = (pageNum - 1) * pageSize;
        
        // 查询总记录数
        int total = quartzJobLogMapper.countByCondition(jobId, jobName, jobGroup, status, startTime, endTime);
        
        // 查询分页数据
        List<QuartzJobLog> logs = quartzJobLogMapper.selectByCondition(
                jobId, jobName, jobGroup, status, startTime, endTime, offset, pageSize);
        
        // 封装分页结果
        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("list", logs);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        result.put("pages", (total + pageSize - 1) / pageSize);
        
        return result;
    }

    @Override
    public void deleteJobLog(Long logId) {
        try {
            quartzJobLogMapper.deleteById(logId);
        } catch (Exception e) {
            log.error("删除任务日志失败", e);
            throw new RuntimeException("删除任务日志失败", e);
        }
    }

    @Override
    public void cleanJobLogs(Date beforeDate) {
        try {
            int count = quartzJobLogMapper.deleteBeforeDate(beforeDate);
            log.info("清理任务日志成功，共清理{}条记录", count);
        } catch (Exception e) {
            log.error("清理任务日志失败", e);
            throw new RuntimeException("清理任务日志失败", e);
        }
    }
} 